package com.chaos.eurekaproducer.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @author liaopeng
 * @title: MyFilter
 * @projectName eureka-producer
 * @description: 自定义过滤器
 * @date 2021/9/22下午8:41
 */
@WebFilter(filterName = "myFilter",urlPatterns = "/*")
public class MyFilter implements Filter {

    /**
     * filter对象只会创建一次，init方法也只会执行一次
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化方法");
    }

    /**
     *  主要的业务代码编写方法，可以多次重复调用
     * @param request
     * @param response
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("我是过滤器的执行方法，客户端向Servlet发送的请求被我拦截到了");
        filterChain.doFilter(request, response);
        System.out.println("我是过滤器的执行方法，Servlet向客户端发送的响应被我拦截到了");
    }

    /**
     * 在销毁filter时自动调用
     */
    @Override
    public void destroy() {
        System.out.println("过滤器被销毁时调用");
    }
}
